# Contributor: Sören Tempel <soeren+alpine@soeren-tempel.net>
# Maintainer: Valery Kartel <valery.kartel@gmail.com>
pkgname=weston
pkgver=10.0.1
pkgrel=0
_libname=lib$pkgname
_libdir=$_libname-${pkgver%%.*}
pkgdesc="The reference Wayland server"
url="https://wayland.freedesktop.org/"
# riscv64 disabled due to missing rust in recursive dependency
arch="all !s390x !ppc64le !riscv64"
license="MIT"
makedepends="wayland-protocols libxkbcommon-dev xkeyboard-config
	libinput-dev libunwind-dev mtdev-dev libxcursor-dev glu-dev
	pango-dev colord-dev libwebp-dev libva-dev dbus-dev
	linux-pam-dev wayland-dev libevdev-dev libjpeg-turbo-dev
	freerdp-dev lcms2-dev gstreamer-dev gst-plugins-base-dev meson
	elogind-dev
	"
_cms="cms-colord cms-static"
_shell="shell-desktop shell-fullscreen shell-ivi"
_client="info terminal wcap-decode"
_backend="backend-drm backend-headless
	backend-x11 backend-wayland backend-rdp
	"
for _sub in $_cms $_shell $_client $_backend; do
	subpackages="$subpackages $pkgname-$_sub:_sub"
done
subpackages="$pkgname-dev $pkgname-doc $subpackages
	$pkgname-clients $_libname-desktop:_libd $_libname:libs
	$pkgname-xwayland $pkgname-desktop-x11:_x11:noarch
	"
source="https://gitlab.freedesktop.org/wayland/weston/-/releases/$pkgver/downloads/weston-$pkgver.tar.xz"
# weston-launch requires suid
options="!check suid"
install="$pkgname.pre-install"

build() {
	abuild-meson \
		-Dlauncher-logind=true \
		-Dsystemd=false \
		-Dpipewire=false \
		build
	meson compile ${JOBS:+-j ${JOBS}} -C build
}

# Does not run through, see also:
# https://github.com/alpinelinux/aports/pull/1689
check() {
	ninja -C "$builddir"/build test
}

package() {
	DESTDIR="$pkgdir" ninja -C "$builddir"/build install

	mkdir -p "$pkgdir"/usr/lib
	mv "$pkgdir"/usr/share/pkgconfig/* "$pkgdir"/usr/lib/pkgconfig/
}

libs() {
	depends="xkeyboard-config"
	default_libs
}

_libd() {
	pkgdesc="Desktop shells abstraction library for libweston compositors"
	mkdir -p "$subpkgdir"/usr/lib
	mv "$pkgdir"/usr/lib/*desktop* "$subpkgdir"/usr/lib
}

clients() {
	pkgdesc="Weston example clients"
	mkdir -p "$subpkgdir"/usr/bin
	mv "$pkgdir"/usr/bin/$pkgname-* "$subpkgdir"/usr/bin
}

xwayland() {
	pkgdesc="Wayland module to run X Clients"
	mkdir -p "$subpkgdir"/usr/lib/$_libdir
	mv "$pkgdir"/usr/lib/$_libdir/xwayland.so \
		"$subpkgdir"/usr/lib/$_libdir
}

_x11() {
	pkgdesc="Weston desktop helper pack (x11 backend)"
	depends="$pkgname $pkgname-shell-desktop $pkgname-xwayland
		$pkgname-backend-x11 $pkgname-terminal"
	mkdir -p "$subpkgdir"
}

_sub() {
	local name path
	case $subpkgname in
	*-cms-*)
		name=${subpkgname#$pkgname-cms-}
		path=/usr/lib/$pkgname
		pkgdesc="Weston CMS module: $name"
		name=cms-$name.so
		;;
	*-shell-*)
		name=${subpkgname#$pkgname-shell-}
		path=/usr/lib/$pkgname
		pkgdesc="Weston shell: $name"
		name=*$name-shell*
		;;
	*-backend-*)
		name=${subpkgname#$pkgname-backend-}
		path=/usr/lib/$_libdir
		pkgdesc="Weston backend: $name"
		name=$name-backend.so
		grep -q cairo "$pkgdir"/$path/$name && depends="mesa-dri-swrast"
		;;
	*)
		name=$subpkgname
		path=/usr/bin
		[ -x "$pkgdir"/$path/$name ] || name=${subpkgname#$pkgname-}
		pkgdesc="Weston client: $name"
		;;
	esac
	mkdir -p "$subpkgdir"/$path
	mv "$pkgdir"/$path/$name "$subpkgdir"/$path
}

sha512sums="
688d843096a95b463161b98e85ca3443b31ba2ee49fc8d456a2780cc96f576e9d097054e9f361997a66e9cc8373cf29e406a742dae3884b3b50b26acf1710c8c  weston-10.0.1.tar.xz
"
